﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listBatchUnsubscribeOutput : Api_BaseOutput
   {
      public listBatchUnsubscribeResults result { get; set; }
      public listBatchUnsubscribeParms inputParms { get; private set; }
      //
      public listBatchUnsubscribeOutput( listBatchUnsubscribeInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new listBatchUnsubscribeResults();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Unsubscribed: {0}, Errors: {1}", result.success_count, result.error_count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }
      // ---- xmlrpc
      internal void Format( x_listBatchUnsubscribe rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }
      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      protected void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int i;
            listBatchUnsubscribeResults results = new listBatchUnsubscribeResults();
            int.TryParse( xml.Element( "success_count" ).Value, out i ); results.success_count = i;
            int.TryParse( xml.Element( "error_count" ).Value, out i ); results.error_count = i;
            IEnumerable<XElement> errors = xml.Elements( "errors" ).Elements( "struct" );
            foreach ( XElement entry in errors )
            {
               listBatchUnsubscribeError err = new listBatchUnsubscribeError();
               int.TryParse( entry.Element( "code" ).Value, out i ); err.code = i;
               err.message = entry.Element( "message" ).Value;
               err.email = entry.Element( "email" ).Value;
               results.errors.Add( err );
            }
            this.result = results;
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      protected void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listBatchUnsubscribe jsonResult = jss.Deserialize<x_listBatchUnsubscribe>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ------------------------------------------------------------------------ FormatResults
      private listBatchUnsubscribeResults FormatResults( x_listBatchUnsubscribe x_results )
      {
         listBatchUnsubscribeResults results = new listBatchUnsubscribeResults();
         //
         results.success_count = x_results.success_count;
         results.error_count = x_results.error_count;
         foreach ( x_listBatchUnsubscribeError x_error in x_results.errors )
         {
            listBatchUnsubscribeError error = new listBatchUnsubscribeError
            {
               email = x_error.email,
               code = x_error.code,
               message = x_error.message
            };
            results.errors.Add( error );
         }
         //
         return results;
      }

      #endregion
   }
}
